//
//  AddViewController.m
//  網易
//
//  Created by 赵天宇 on 16/9/19.
//  Copyright © 2016年 赵天宇. All rights reserved.
//

#import "AddViewController.h"
#import "AppDelegate.h"
@interface AddViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UITextViewDelegate>{
    UIImagePickerController *pickCon;
    UITextView *textV;
    UILabel *lable;
    UIImageView *imageV;
    UILabel *lab;
    UILabel *labImage;
    UILabel *textLable;
    UILabel *line;
    UIButton *btn;
    AppDelegate *app;
}

@end

@implementation AddViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.hidesBackButton=YES;
    app = [UIApplication sharedApplication].delegate;
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIBarButtonItem *rigthItem = [[UIBarButtonItem alloc]initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(fabiao)];
    self.navigationItem.rightBarButtonItem=rigthItem;
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
    self.navigationItem.title=@"新闻编辑";
    self.navigationController.navigationBar.barStyle=UIBarStyleBlack;
    self.navigationController.navigationBar.barTintColor=[UIColor redColor];
    pickCon = [[UIImagePickerController alloc]init];
    pickCon.delegate=self;
//    lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 70, 100, 50)];
    lable.backgroundColor = [UIColor cyanColor];
    lable.layer.masksToBounds=YES;
    lable.layer.cornerRadius=25;
    lable.text=@"添加标题:";
    lable.textAlignment=UITextAlignmentCenter;
    [self.view addSubview:lable];
    textV = [[UITextView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 250)];
    textV.font = [UIFont systemFontOfSize:18];
    textV.delegate=self;
    [self.view addSubview:textV];
    lab = [[UILabel alloc]initWithFrame:CGRectMake(10, 67, 100, 20)];
    lab.text=@"说点什么吧...";
    lab.font = [UIFont systemFontOfSize:14];
    lab.textColor=[UIColor grayColor];
    lab.backgroundColor=[UIColor clearColor];
    [self.view addSubview:lab];
    labImage = [[UILabel alloc]initWithFrame:CGRectMake(10, 180, 100, 100)];
    labImage.layer.borderWidth=1;
    labImage.layer.borderColor=[[UIColor cyanColor]CGColor];
    [self.view addSubview:labImage];
    btn = [UIButton buttonWithType:0];
    [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    btn.frame=CGRectMake(0, 0, 80, 80);
    btn.center=labImage.center;
    [self.view addSubview:btn];
    [btn setBackgroundImage:[UIImage imageNamed:@"night_top_navigation_normal@2x.png"] forState:0];
    textLable = [[UILabel alloc]initWithFrame:CGRectMake(120, 220, 100, 20)];
    textLable.text=@"添加照片,视频";
    textLable.textColor=[UIColor grayColor];
    textLable.font =[UIFont systemFontOfSize:14];
    [self.view addSubview:textLable];
    line =[[UILabel alloc]initWithFrame:CGRectMake(0, 314, self.view.frame.size.width, 1)];
    line.backgroundColor =[UIColor cyanColor];
    [self.view addSubview:line];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem=leftItem;
    self.navigationItem.leftBarButtonItem.tintColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
}
-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)fabiao{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"确定发布" message:@"" preferredStyle:UIAlertActionStyleDefault];
    
    UIAlertAction *action_1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [app.arrTitle addObject:textV.text];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    UIAlertAction *action_2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVC addAction:action_1];
    [alertVC addAction:action_2];
    [self presentViewController:alertVC animated:YES completion:nil];
    
}
-(void)add{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提醒" message:@"请选择照片来源" preferredStyle:UIAlertControllerStyleActionSheet];
    
    UIAlertAction *actionCarmer = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        pickCon.sourceType = UIImagePickerControllerSourceTypeCamera;
        pickCon.allowsEditing = YES;
        [self presentViewController:pickCon animated:YES completion:nil];
    }];
    
    UIAlertAction *actionPicLibrary = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        pickCon.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                pickCon.allowsEditing = YES;
        [self presentViewController:pickCon animated:YES completion:nil];
    }];
    
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertVC addAction:actionCarmer];
    [alertVC addAction:actionPicLibrary];
    [alertVC addAction:actionCancel];
    
    [self presentViewController:alertVC animated:YES completion:nil];

}
-(void)textViewDidChange:(UITextView *)textView{
    if(textView.text.length==0){
        lab.text=@"说点什么吧...";
    }else{
        lab.text=@" ";
    }
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [btn setBackgroundImage:[info objectForKey:UIImagePickerControllerEditedImage] forState:0];
   [app.arrImage addObject: [info objectForKey:UIImagePickerControllerEditedImage]];
    [self dismissViewControllerAnimated:YES completion:nil];


}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
